<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\Gamearea   as GameareaModel;
use think\Config;
use think\Db;

/**
 * 区服管理
 * Class AdminUser
 * @package app\admin\controller
 */
class Garea extends AdminBase
{
    protected $gamearea_model;	
    protected function initialize()
    {
        parent::initialize();
		$this->gamearea_model  = new GameareaModel();
    }

    /**
     * 区服管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function index($keyword ='', $page = 1)
    {
        $map = [];
        if($keyword)
        {
           $map[] = ['name','like','%$keyword%'];
        }
        $game_garea = Db::name('gamearea')->where($map)->order('gid DESC')->paginate(15);
        return $this->fetch('index', ['game_garea' => $game_garea, 'keyword' => $keyword]);
    }
	
    /**
     * 添加区服
     * @return mixed
     */
    public function add()
    {
		
        return $this->fetch();
    }
	
    /**
     * 生成接口key
     */
    public function addkey()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
			if ($data['type'] == 'loginkey'){
				$newkey = md5(sha1(session_id() . time()). Config::get('lsalt'));
				$result = [
				            'code'   => 1,
							'msg' => $newkey
				];
			}elseif ($data['type'] == 'paykey'){
				$newkey = md5(sha1(session_id() . time()). Config::get('psalt'));
				$result = [
				           'code'   => 1,
				           'msg' => $newkey
				];
			}
			return json($result);
        }
    }
	
    /**
     * 保存区服
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'GameArea');
            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                $data['open_time']=time($data['open_time']);	
                if (Db::name('gamearea')->insert($data)) {
                    $this->success('保存成功','/admin/garea');
                } else {
                    $this->error('保存失败');
                }
            }
        }
    }
	
    /**
     * 编辑区服
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
    	$config=Db::name('admin_config')->where('name','gamearea_db_config')->find();
		$data_info=explode(',', $config['options']);
		$data_name=explode('，', $config['value']);
		$info=array_combine($data_name,$data_info);
		$this->assign('info',$info);
		$garea =Db::name('gamearea')->find($id);
        $garea['open_time']=date('Y-m-d H:i:s', $garea['open_time']);
        return $this->fetch('edit', ['garea' => $garea]);
    }

    /**
     * 更新区服
     * @param $id
     */
    public function update($id)
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'GameArea');
            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
            	$data['open_time']=time($data['open_time']);	
            	$data['info']=serialize($data['info']);
                if ($game=Db::name('gamearea')->where('id',$id)->update($data)) {
                    $this->success('更新成功','/admin/garea');
                } else {
                    $this->error('更新失败');
                }
            }
        }
    }
	
    /**
     * 更改状态
     * @param $id
     */
    public function check($id,$type,$status)
    {
        if ($this->request->isPost()) {
			$game               = $this->garea_model->find($id);
			$game->id           = $id;
			if ($type == 'show'){
			$game->show         = $status;
			}elseif($type == 'status'){
			$game->status       = $status;	
			}
            if ($game->save() !== false) {
                $this->success('更新状态成功');
            } else {
                $this->error('更新状态成功');
            }
        }
    }	

}